package 顺序表.练习.扑克牌游戏;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardList {
    private static final String[] PATTERN = {"♣","♦","♥","♠"};

    //生成扑克
    public static ArrayList<Card> makeCardList(){
        ArrayList<Card> list = new ArrayList<>();
        for (int i = 0; i < PATTERN.length; i++) {
            for (int j = 0; j <=13 ; j++) {
                String pattern = PATTERN[i];
                int num = j;
                Card card = new Card(pattern,num);
                list.add(card);
            }
        }
        return list;
    }

    //打乱扑克
    public static ArrayList<Card> shuffle(ArrayList<Card> list){
        Random random = new Random();
        for (int i =list.size()-1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(list,i,index);
        }
        return list;
    }
    public static List<Card> swap(ArrayList<Card> list,int i,int j){
        Card tmp = list.get(i);
        list.set(i,list.get(j));
        list.set(j,tmp);
        return list;
    }


    public static void main(String[] args) {
        ArrayList<Card> list = makeCardList();
        System.out.println("打乱前："+list);
        //打乱
        shuffle(list);
        System.out.println("打乱后："+list);
        //发牌
        ArrayList<ArrayList<Card>> users  = new ArrayList<>();
        ArrayList<Card> user1 = new ArrayList<>();
        ArrayList<Card> user2 = new ArrayList<>();
        ArrayList<Card> user3 = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);

        //轮流发，一人一张
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                users.get(j).add(list.get(0));
                list.remove(0);
            }
        }
        //打印
        for (int i = 0; i < 3; i++) {
            System.out.println("第"+(i+1)+"位用户手中的牌"+users.get(i));
        }
    }
}
